package app.common.utils;

import app.common.utils.dingtalk.domain.SearchCondition;
import com.aliyun.dingtalkyida_1_0.models.BatchGetFormDataByIdListResponseBody;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 形式跑龙套
 * @author feizns
 * @since 2023/07/21
 */
public abstract class FormsUtils {

    private static final DecimalFormat dec = new DecimalFormat("0.0000");

    /**
     * 日期缓存
     */
    private static DateCache dateCache = new DateCache();

    /**
     * 得到价格缓存
     * @param no 没有
     * @return {@link BigDecimal}
     */
    public static BigDecimal getPriceCache(String no) {
        if ( !dateCache.isNow() )
            dateCache = new DateCache();
        BigDecimal decimal = dateCache.get(no);
        if ( decimal != null ) {
            BigDecimal price = getPrice(no);
            dateCache.put(no, price);
        }
        return dateCache.get(no);
    }

    /**
     * 得到价格
     * @param no 没有
     * @return {@link BigDecimal}
     */
    public static BigDecimal getPrice(String no) {
        List<String> list = YdApi.yiDaUtils.forms.instanceIds("FORM-3J866L81O1NBM6G6FG42P98YQOC73EETBAVIL0", SearchCondition.textEq("textField_livac0ce", no));
        try {
            for (String id : list) {
                Number price = (Number) YdApi.yiDaUtils.forms.getById(id).getFormData().get("numberField_livac0ck");
                if ( price != null )
                    return new BigDecimal(dec.format(price.doubleValue()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            //ignore
        }
        return BigDecimal.ZERO;
    }

    /**
     * 工序列表
     * @param merchantCode merchantCode
     * @return {@link List}<{@link BatchGetFormDataByIdListResponseBody.BatchGetFormDataByIdListResponseBodyResult}>
     */
    public static List<BatchGetFormDataByIdListResponseBody.BatchGetFormDataByIdListResponseBodyResult> processList(String merchantCode) {
        return YdApi.yiDaUtils.forms.getList("FORM-FDA66N81XTS94LNBB4C9879UH2HC27IJGAAGL1",
                SearchCondition.textEq("textField_lfgk4kjq", merchantCode)
        );
    }

    /**
     * 日期缓存
     * @author feizns
     * @since 2023/07/28
     */
    public static class DateCache {

        /**
         * 日期
         */
        private final LocalDate date = LocalDate.now();

        /**
         * 缓存
         */
        private final Map<String, BigDecimal> cache = new HashMap<>();

        /**
         * 得到
         * @param no 没有
         * @return {@link BigDecimal}
         */
        public BigDecimal get(String no) {
            return cache.get(no);
        }

        /**
         * 把
         * @param no 没有
         * @param value 价值
         */
        public void put(String no, BigDecimal value) {
            cache.put(no, value);
        }

        /**
         * 是否是现在
         * @return boolean
         */
        public boolean isNow() {
            return LocalDate.now().isEqual(date);
        }

    }

}
